package com.zmn.oms.business.impl.operator;

import com.zmn.base.common.data.common.dro.company.CompanyDRO;
import com.zmn.base.common.data.dubbo.interfaces.company.CompanyListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.oms.business.interfaces.operator.OrderWorkOperatorBService;
import com.zmn.oms.common.annotation.OrderWorkChangeSync;
import com.zmn.oms.common.dto.QueueOrderNodeNotifyDTO;
import com.zmn.oms.model.dto.operator.OrderWorkOperatorDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.operator.OrderWorkOperator;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.operator.OrderWorkOperatorService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：保存工单创建人部门
 *
 * @author liuying
 * @date 2021/10/18 16:46
 */
@Service
public class OrderWorkOperatorBServiceImpl implements OrderWorkOperatorBService {

    @Resource
    private OrderWorkOperatorService orderWorkOperatorService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Reference(version = com.zmn.mcc.dubbo.dto.DubboConsts.INTERFACE_VERSION, check = false)
    private StaffListRemoteService staffListRemoteService;
    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    protected CompanyListRemoteService companyListRemoteService;


    @Override
    public void addOperatorByNodeNotify(QueueOrderNodeNotifyDTO queueOrderNodeNotifyDTO) {

        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(queueOrderNodeNotifyDTO.getOrderId());

        Date now = DateUtil.getNow();
        OrderWorkOperator orderWorkOperator = new OrderWorkOperator();
        orderWorkOperator.setWorkId(queueOrderNodeNotifyDTO.getWorkId());
        orderWorkOperator.setOrderId(queueOrderNodeNotifyDTO.getOrderId());
        orderWorkOperator.setCreater(queueOrderNodeNotifyDTO.getOperator());
        orderWorkOperator.setUpdater(queueOrderNodeNotifyDTO.getOperator());
        orderWorkOperator.setCreateTime(now);
        orderWorkOperator.setUpdateTime(now);

        // 查询员工部门
        if (NumberUtil.isNotNullOrZero(queueOrderNodeNotifyDTO.getOperatorId())
                && Objects.equals(queueOrderNodeNotifyDTO.getOperatorType(), GlobalConsts.OPERATE_USER_TYPE_STAFF)) {
            ResponseDTO<StaffDRO> staff = staffListRemoteService.getStaff(queueOrderNodeNotifyDTO.getOperatorId().intValue());
            if (Objects.nonNull(staff.getData())) {
                orderWorkOperator.setCreatorDept(staff.getData().getDeptId());
            }
        }

        if (NumberUtil.isNotNullOrZero(orderDetail.getGridCompanyId())
                && NumberUtil.isNotNullOrZero(orderDetail.getGridCompanyId())) {
            // 获取公司信息
            ResponseDTO<CompanyDRO> companyResponseDTO = companyListRemoteService.getCompanyDROById(orderDetail.getGridCompanyId());
            if (companyResponseDTO.isSuccess() && Objects.nonNull(companyResponseDTO.getData())) {
                CompanyDRO companyDRO = companyResponseDTO.getData();

                orderWorkOperator.setGridOrgOneId(companyDRO.getPlat());
                orderWorkOperator.setGridOrgTwoId(companyDRO.getOrgTwoId());
                orderWorkOperator.setGridOrgThreeId(companyDRO.getOrgThrId());
                orderWorkOperator.setGridOrgFourId(orderDetail.getGridManageCompanyId());
            }
        }

        try {
            orderWorkOperatorService.insert(orderWorkOperator);
        } catch (DuplicateKeyException e) {
            // 主键重复插入失败后执行修改
            orderWorkOperator.setCreater(null);
            orderWorkOperator.setCreateTime(null);
            orderWorkOperatorService.updateOrderWorkOperator(orderWorkOperator);
        }
    }

    @Override
    @OrderWorkChangeSync
    public void insertOrUpdateOperator(OrderWorkOperatorDTO dto) {
        OrderWorkOperator orderWorkOperator = orderWorkOperatorService.findByOrderIdAndWorkId(dto.getOrderId(), dto.getWorkId());
        Date date = Optional.ofNullable(dto.getOperateTime()).orElse(DateUtil.getNow());
        if (Objects.isNull(orderWorkOperator)) {
            orderWorkOperator = new OrderWorkOperator();
            orderWorkOperator.setWorkId(dto.getWorkId());
            orderWorkOperator.setOrderId(dto.getOrderId());
            orderWorkOperator.setCreatorDept(dto.getCreatorDept());
            orderWorkOperator.setGridOrgOneId(dto.getGridOrgOneId());
            orderWorkOperator.setGridOrgTwoId(dto.getGridOrgTwoId());
            orderWorkOperator.setGridOrgThreeId(dto.getGridOrgThreeId());
            orderWorkOperator.setGridOrgFourId(dto.getGridOrgFourId());
            orderWorkOperator.setCreater(dto.getOperator());
            orderWorkOperator.setUpdater(dto.getOperator());
            orderWorkOperator.setCreateTime(date);
            orderWorkOperator.setUpdateTime(date);
            try {
                orderWorkOperatorService.insert(orderWorkOperator);
            } catch (DuplicateKeyException e) {
                // 主键重复插入失败后执行修改
                orderWorkOperator.setCreater(null);
                orderWorkOperator.setCreateTime(null);
                orderWorkOperatorService.updateOrderWorkOperator(orderWorkOperator);
            }
            return;
        }

        orderWorkOperator = new OrderWorkOperator();
        orderWorkOperator.setWorkId(dto.getWorkId());
        orderWorkOperator.setOrderId(dto.getOrderId());
        orderWorkOperator.setCreatorDept(dto.getCreatorDept());
        orderWorkOperator.setGridOrgOneId(dto.getGridOrgOneId());
        orderWorkOperator.setGridOrgTwoId(dto.getGridOrgTwoId());
        orderWorkOperator.setGridOrgThreeId(dto.getGridOrgThreeId());
        orderWorkOperator.setGridOrgFourId(dto.getGridOrgFourId());
        orderWorkOperator.setUpdater(dto.getOperator());
        orderWorkOperator.setUpdateTime(date);
        orderWorkOperatorService.updateOrderWorkOperator(orderWorkOperator);
    }
}
